package com.lhkj.ct.meta.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lhkj.ct.base.model.dto.PageQuery;
import com.lhkj.ct.base.enums.GlobalStatus;
import com.lhkj.ct.base.model.Option;
import com.lhkj.ct.base.mybatis.extension.service.impl.IServiceImpl;
import com.lhkj.ct.base.utils.Asserts;
import com.lhkj.ct.base.utils.OptionUtil;
import com.lhkj.ct.meta.admin.model.entity.TblPost;
import com.lhkj.ct.meta.admin.service.SysPostService;
import com.lhkj.ct.meta.admin.mapper.SysPostMapper;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 岗位表 服务实现类
 * </p>
 *
 * @author jobob
 * @since 2023-09-08
 */
@Service
public class SysPostServiceImpl extends IServiceImpl<SysPostMapper, TblPost> implements SysPostService {

    @Override
    public IPage<TblPost> selectPostPage(TblPost post, PageQuery pageQuery) {
        return getBaseMapper().selectPostPage(generatePage(pageQuery), post);
    }

    @Override
    public List<Option<String>> listOptions(String organId) {
        List<TblPost> list = this.getBaseMapper().selectList(new LambdaQueryWrapper<TblPost>()
                .select(TblPost::getId, TblPost::getPostName)
                .eq(TblPost::getStatus, GlobalStatus.NORMAL.getValue())
                .eq(TblPost::getOrganId, organId)
                .orderByAsc(TblPost::getPostSort)
        );
        return OptionUtil.buildOption(list, TblPost::getId,TblPost::getPostName);
    }

    @Override
    public int removeByIds(Collection<? extends Serializable> idList) {
        for (Serializable serializable : idList) {
            Asserts.assertFalse(checkPostExistUser((String) serializable), "岗位已分配，不允许删除");
        }
        return getBaseMapper().deleteBatchIds(idList);
    }

    @Override
    public boolean checkPostExistUser(String postId) {
        return this.getBaseMapper().checkPostExistUser(postId) > 0;
    }
}
